In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as mpl
import seaborn as sb
import plotly.express as pl
%matplotlib inline
In [2]:
df1 = pd.read_csv(r"C:\Users\grvku\OneDrive\Desktop\Python\Week 3\Automobile.csv")
In [3]:
df1
Out[3]:
symboling normalized_losses make fuel_type aspiration number_of_doors body_style drive_wheels engine_location wheel_base ... engine_size fuel_system bore stroke compression_ratio horsepower peak_rpm city_mpg highway_mpg price
0 3 168 alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 13495
1 3 168 alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 16500
2 1 168 alfa-romero gas std two hatchback rwd front 94.5 ... 152 mpfi 2.68 3.47 9.0 154 5000 19 26 16500
3 2 164 audi gas std four sedan fwd front 99.8 ... 109 mpfi 3.19 3.40 10.0 102 5500 24 30 13950
4 2 164 audi gas std four sedan 4wd front 99.4 ... 136 mpfi 3.19 3.40 8.0 115 5500 18 22 17450
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
196 -1 95 volvo gas std four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 9.5 114 5400 23 28 16845
197 -1 95 volvo gas turbo four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 8.7 160 5300 19 25 19045
198 -1 95 volvo gas std four sedan rwd front 109.1 ... 173 mpfi 3.58 2.87 8.8 134 5500 18 23 21485
199 -1 95 volvo diesel turbo four sedan rwd front 109.1 ... 145 idi 3.01 3.40 23.0 106 4800 26 27 22470
200 -1 95 volvo gas turbo four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 9.5 114 5400 19 25 22625

201 rows × 26 columns

In [4]:
df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 201 entries, 0 to 200
Data columns (total 26 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   symboling            201 non-null    int64  
 1   normalized_losses    201 non-null    int64  
 2   make                 201 non-null    object 
 3   fuel_type            201 non-null    object 
 4   aspiration           201 non-null    object 
 5   number_of_doors      201 non-null    object 
 6   body_style           201 non-null    object 
 7   drive_wheels         201 non-null    object 
 8   engine_location      201 non-null    object 
 9   wheel_base           201 non-null    float64
 10  length               201 non-null    float64
 11  width                201 non-null    float64
 12  height               201 non-null    float64
 13  curb_weight          201 non-null    int64  
 14  engine_type          201 non-null    object 
 15  number_of_cylinders  201 non-null    object 
 16  engine_size          201 non-null    int64  
 17  fuel_system          201 non-null    object 
 18  bore                 201 non-null    float64
 19  stroke               201 non-null    float64
 20  compression_ratio    201 non-null    float64
 21  horsepower           201 non-null    int64  
 22  peak_rpm             201 non-null    int64  
 23  city_mpg             201 non-null    int64  
 24  highway_mpg          201 non-null    int64  
 25  price                201 non-null    int64  
dtypes: float64(7), int64(9), object(10)
memory usage: 41.0+ KB
In [5]:
df1.describe(include='all').T
Out[5]:
count unique top freq mean std min 25% 50% 75% max
symboling 201.0 NaN NaN NaN 0.840796 1.254802 -2.0 0.0 1.0 2.0 3.0
normalized_losses 201.0 NaN NaN NaN 125.189055 33.572966 65.0 101.0 122.0 150.0 256.0
make 201 22 toyota 32 NaN NaN NaN NaN NaN NaN NaN
fuel_type 201 2 gas 181 NaN NaN NaN NaN NaN NaN NaN
aspiration 201 2 std 165 NaN NaN NaN NaN NaN NaN NaN
number_of_doors 201 2 four 114 NaN NaN NaN NaN NaN NaN NaN
body_style 201 5 sedan 94 NaN NaN NaN NaN NaN NaN NaN
drive_wheels 201 3 fwd 118 NaN NaN NaN NaN NaN NaN NaN
engine_location 201 2 front 198 NaN NaN NaN NaN NaN NaN NaN
wheel_base 201.0 NaN NaN NaN 98.797015 6.066366 86.6 94.5 97.0 102.4 120.9
length 201.0 NaN NaN NaN 174.200995 12.322175 141.1 166.8 173.2 183.5 208.1
width 201.0 NaN NaN NaN 65.889055 2.101471 60.3 64.1 65.5 66.6 72.0
height 201.0 NaN NaN NaN 53.766667 2.447822 47.8 52.0 54.1 55.5 59.8
curb_weight 201.0 NaN NaN NaN 2555.666667 517.296727 1488.0 2169.0 2414.0 2926.0 4066.0
engine_type 201 6 ohc 145 NaN NaN NaN NaN NaN NaN NaN
number_of_cylinders 201 7 four 157 NaN NaN NaN NaN NaN NaN NaN
engine_size 201.0 NaN NaN NaN 126.875622 41.546834 61.0 98.0 120.0 141.0 326.0
fuel_system 201 8 mpfi 92 NaN NaN NaN NaN NaN NaN NaN
bore 201.0 NaN NaN NaN 3.329701 0.268166 2.54 3.15 3.31 3.58 3.94
stroke 201.0 NaN NaN NaN 3.261741 0.317875 2.07 3.11 3.29 3.46 4.17
compression_ratio 201.0 NaN NaN NaN 10.164279 4.004965 7.0 8.6 9.0 9.4 23.0
horsepower 201.0 NaN NaN NaN 103.263682 37.389372 48.0 70.0 95.0 116.0 262.0
peak_rpm 201.0 NaN NaN NaN 5121.393035 479.624905 4150.0 4800.0 5200.0 5500.0 6600.0
city_mpg 201.0 NaN NaN NaN 25.179104 6.42322 13.0 19.0 24.0 30.0 49.0
highway_mpg 201.0 NaN NaN NaN 30.686567 6.81515 16.0 25.0 30.0 34.0 54.0
price 201.0 NaN NaN NaN 13207.129353 7947.066342 5118.0 7775.0 10295.0 16500.0 45400.0
In [6]:
df1.shape
Out[6]:
(201, 26)
In [7]:
seaborn1 = sb.histplot(data=df1,x='normalized_losses')
In [8]:
mpl.title('Histogram')
mpl.xlabel('Price of car')
mpl.ylim(0,70)
mpl.ylabel('Number')
sb.histplot(data=df1, x='price',bins=20,color='green')
Out[8]:
<AxesSubplot:title={'center':'Histogram'}, xlabel='Price of car', ylabel='Number'>
In [9]:
#histogram
sb.histplot(data=df1,x='price',color='green',binwidth=203);

In addition to the bars, we can also add a density estimate by setting the kde parameter to True.

  • Kernel Density Estimation, or KDE, visualizes the distribution of data over a continuous interval.
  • The conventional scale for KDE is: Total frequency of each bin × Probability
In [10]:
sb.histplot(data=df1, x='price', color='green', kde=True, bins=20)
Out[10]:
<AxesSubplot:xlabel='price', ylabel='Count'>

hue (compare the distributions of several groups.)¶

In [11]:
sb.histplot(data=df1,x='price',hue='engine_type',color='green',kde=True);
In [12]:
sb.histplot(data=df1,x='price',hue='number_of_doors',kde=True)
Out[12]:
<AxesSubplot:xlabel='price', ylabel='Count'>
In [13]:
g = sb.FacetGrid(data=df1,col='engine_type')
g.map(sb.histplot,'price');
In [14]:
sb.boxplot(data=df1, x='price')
df1['price'].describe()
Out[14]:
count      201.000000
mean     13207.129353
std       7947.066342
min       5118.000000
25%       7775.000000
50%      10295.000000
75%      16500.000000
max      45400.000000
Name: price, dtype: float64
In [15]:
sb.boxplot(data=df1,x='price',y='engine_type',hue='fuel_type')
print(df1.value_counts('engine_type'))
df1.groupby(['engine_type'])['price'].describe().T
engine_type
ohc      145
ohcf      15
ohcv      13
dohc      12
l         12
rotor      4
dtype: int64
Out[15]:
engine_type dohc l ohc ohcf ohcv rotor
count 12.000000 12.000000 145.000000 15.00000 13.000000 4.000000
mean 18116.416667 14627.583333 11567.358621 13738.60000 25098.384615 13020.000000
std 7963.955772 3673.598940 6445.408602 10930.47703 11442.925722 2079.062289
min 9298.000000 5151.000000 5195.000000 5118.00000 13499.000000 10945.000000
25% 15141.250000 13010.000000 7349.000000 7533.00000 16500.000000 11620.000000
50% 16249.000000 16105.000000 9095.000000 9233.00000 19699.000000 12745.000000
75% 18267.500000 16943.750000 13415.000000 11476.50000 35056.000000 14145.000000
max 35550.000000 18150.000000 41315.000000 37028.00000 45400.000000 15645.000000
In [16]:
sb.histplot(data=df1, x='price', hue='fuel_type',kde=True);

Bargraph¶

In [17]:
sb.countplot(data=df1 , x='body_style')
df1.value_counts('body_style')
Out[17]:
body_style
sedan          94
hatchback      68
wagon          25
hardtop         8
convertible     6
dtype: int64
In [18]:
sb.countplot(data=df1,x='body_style',hue='fuel_type')
df1.groupby(['body_style'])['fuel_type'].describe()
Out[18]:
count unique top freq
body_style
convertible 6 1 gas 6
hardtop 8 2 gas 7
hatchback 68 2 gas 67
sedan 94 2 gas 79
wagon 25 2 gas 22
In [19]:
sb.countplot(data=df1,x='make')
Out[19]:
<AxesSubplot:xlabel='make', ylabel='count'>
In [20]:
mpl.figure(figsize=(20,7)) #for incresing the size
sb.countplot(data=df1,x='make')
Out[20]:
<AxesSubplot:xlabel='make', ylabel='count'>
In [21]:
mpl.figure(figsize=(20,7))
sb.countplot(data=df1,x='make');
mpl.xticks(rotation=90);
mpl.xlabel('Model')
Out[21]:
Text(0.5, 0, 'Model')

Line plot¶

In [22]:
mpl.figure(figsize=(20,7))
sb.lineplot(data=df1,x='price',y='number_of_cylinders',hue='fuel_type',ci=False,markers=True)
Out[22]:
<AxesSubplot:xlabel='price', ylabel='number_of_cylinders'>

scatterplot¶

In [23]:
mpl.figure(figsize=(20,7))
sb.scatterplot(data=df1,x='horsepower',y='price',hue='fuel_type');
mpl.xticks(rotation=90);

Lmplot (linear model plot)¶

In [24]:
mpl.figure(figsize=(20,7))
sb.lmplot(data=df1,x='horsepower',y='price',hue='fuel_type');
<Figure size 2000x700 with 0 Axes>
In [25]:
sb.lmplot(data=df1,x='horsepower',y='price',hue='fuel_type',ci=False)
Out[25]:
<seaborn.axisgrid.FacetGrid at 0x2cd2b006610>
In [26]:
sb.lmplot(data=df1,x='horsepower',y='price',col='fuel_type',ci=False) #Confidence interval = ci
Out[26]:
<seaborn.axisgrid.FacetGrid at 0x2cd2ce66b50>
In [27]:
df2 = df1[df1['price'] >= 5000]
df2.reset_index(drop=True,inplace=True)
df2.to_csv(r"C:\Users\grvku\OneDrive\Desktop\Python\Week 3\Automobilexbyme.csv")

Jointplot¶

joinpoit will join scatterplot and histogram

We can find univariate as well joint distribution information from the joint plot.

In [28]:
sb.jointplot(data=df1,x='engine_size',y='horsepower');
In [29]:
sb.jointplot(data=df1,x='engine_size',y='horsepower',kind='hex');
mpl.colorbar()
Out[29]:
<matplotlib.colorbar.Colorbar at 0x2cd2a64b340>
In [30]:
sb.jointplot(data=df1,x='engine_size',y='horsepower',kind='kde',fill=True);
In [31]:
sb.jointplot(data=df1,x='engine_size',y='horsepower',kind='reg',ci=False);

We can plot various types of jointplotLinks to an external site. by using the kind attribute. The kind attribute accepts the following keywords:

'kde' for kde plot
'scatter' for scatter plot
'hist' for hist plot
'reg' for regression plot
'hex' for hex plot
In [32]:
import matplotlib.pyplot as mpl
import seaborn as sb
In [33]:
sb.histplot(data=df1,x='horsepower')
Out[33]:
<AxesSubplot:xlabel='horsepower', ylabel='Count'>
In [34]:
!pip install plotly
Requirement already satisfied: plotly in c:\users\grvku\anaconda3\lib\site-packages (5.9.0)
Requirement already satisfied: tenacity>=6.2.0 in c:\users\grvku\anaconda3\lib\site-packages (from plotly) (8.0.1)
In [35]:
import plotly.express as pl
In [36]:
his = pl.histogram(df1,x='horsepower')
his.show()
In [37]:
import matplotlib.pyplot as mpl
import plotly.express as pl
In [38]:
bar1 = pl.bar(df1,x='horsepower',y='price')
bar1.show()
In [39]:
import plotly.express as pl
import matplotlib.pyplot as mpl
In [40]:
pl.scatter(df1,x='price',y='curb_weight')
In [41]:
import matplotlib.pyplot as mpl
import plotly.express as pl
In [42]:
pl.box(df1,y='price',x='fuel_type',points='all')
In [43]:
df1.groupby(['fuel_type'])['price'].describe()
Out[43]:
count mean std min 25% 50% 75% max
fuel_type
diesel 20.0 15838.15000 7759.843713 7099.0 9120.0 13852.5 19375.5 31600.0
gas 181.0 12916.40884 7934.923212 5118.0 7609.0 9989.0 15985.0 45400.0
In [44]:
import plotly.express as pl
import matplotlib.pyplot as mpl
In [45]:
carpxhxh = pl.scatter_3d(df1,x='price',y='horsepower',z='height',color='fuel_type')
carpxhxh.show()
In [46]:
carpxhxh.write_html('carpxhxh.html')
In [53]:
pd.pivot_table(df1,index=['make','fuel_type'],columns=['engine_location'],aggfunc=len,dropna=True)
Out[53]:
aspiration body_style bore city_mpg compression_ratio ... price stroke symboling wheel_base width
engine_location front rear front rear front rear front rear front rear ... front rear front rear front rear front rear front rear
make fuel_type
alfa-romero gas 3.0 Na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0 NaN 4.0 NaN 4.0 NaN
mercury gas 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN ... 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN
mitsubishi gas 13.0 NaN 13.0 NaN 13.0 NaN 13.0 NaN 13.0 NaN ... 13.0 NaN 13.0 NaN 13.0 NaN 13.0 NaN 13.0 NaN
nissan diesel 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN ... 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN
gas 17.0 NaN 17.0 NaN 17.0 NaN 17.0 NaN 17.0 NaN ... 17.0 NaN 17.0 NaN 17.0 NaN 17.0 NaN 17.0 NaN
peugot diesel 5.0 NaN 5.0 NaN 5.0 NaN 5.0 NaN 5.0 NaN ... 5.0 NaN 5.0 NaN 5.0 NaN 5.0 NaN 5.0 NaN
gas 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN ... 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN
plymouth gas 7.0 NaN 7.0 NaN 7.0 NaN 7.0 NaN 7.0 NaN ... 7.0 NaN 7.0 NaN 7.0 NaN 7.0 NaN 7.0 NaN
porsche gas 1.0 3.0 1.0 3.0 1.0 3.0 1.0 3.0 1.0 3.0 ... 1.0 3.0 1.0 3.0 1.0 3.0 1.0 3.0 1.0 3.0
renault gas 2.0 NaN 2.0 NaN 2.0 NaN 2.0 NaN 2.0 NaN ... 2.0 NaN 2.0 NaN 2.0 NaN 2.0 NaN 2.0 NaN
saab gas 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN ... 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN 6.0 NaN
subaru gas 12.0 NaN 12.0 NaN 12.0 NaN 12.0 NaN 12.0 NaN ... 12.0 NaN 12.0 NaN 12.0 NaN 12.0 NaN 12.0 NaN
toyota diesel 3.0 NaN 3.0 NaN 3.0 NaN 3.0 NaN 3.0 NaN ... 3.0 NaN 3.0 NaN 3.0 NaN 3.0 NaN 3.0 NaN
gas 29.0 NaN 29.0 NaN 29.0 NaN 29.0 NaN 29.0 NaN ... 29.0 NaN 29.0 NaN 29.0 NaN 29.0 NaN 29.0 NaN
volkswagen diesel 4.0 NaN 4.0 NaN 4.0 NaN 4.0 NaN 4.0 NaN ... 4.0 NaN 4.0 NaN 4.0 NaN 4.0 NaN 4.0 NaN
gas 8.0 NaN 8.0 NaN 8.0 NaN 8.0 NaN 8.0 NaN ... 8.0 NaN 8.0 NaN 8.0 NaN 8.0 NaN 8.0 NaN
volvo diesel 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN ... 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN 1.0 NaN
gas 10.0 NaN 10.0 NaN 10.0 NaN 10.0 NaN 10.0 NaN ... 10.0 NaN 10.0 NaN 10.0 NaN 10.0 NaN 10.0 NaN

29 rows × 46 columns

In [54]:
pd.crosstab(df1['make'],df1['engine_location'])
Out[54]:
engine_location front rear
make
alfa-romero 3 0
audi 6 0
bmw 8 0
chevrolet 3 0
dodge 9 0
honda 13 0
isuzu 2 0
jaguar 3 0
mazda 17 0
mercedes-benz 8 0
mercury 1 0
mitsubishi 13 0
nissan 18 0
peugot 11 0
plymouth 7 0
porsche 1 3
renault 2 0
saab 6 0
subaru 12 0
toyota 32 0
volkswagen 12 0
volvo 11 0
In [ ]: